package com.dstz.demo.rest.controller;

import com.dstz.base.api.aop.annotion.CatchErr;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.rest.BaseController;
import com.dstz.demo.core.manager.ParamCompareManager;
import com.dstz.demo.core.model.Matecode;
import com.dstz.demo.core.model.ParamCompare;
import com.dstz.form.api.constant.FormStatusCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * paramCompare 控制器类<br/>
 * @author  aschs
 * </pre>
 */
@RestController
@RequestMapping("/sys/paramCompare")
public class ParamCompareController extends BaseController<ParamCompare>{
	@Resource
	ParamCompareManager paramCompareManager;
	
	
	@Override
	protected String getModelDesc() {
		return "paramCompare";
	}

//
//	/**
//	 * 材料参数对比
//	 * @param ids
//	 * @return
//	 * @throws Exception
//	 */
//	@PostMapping("/compareParams/{ids}")
//	@CatchErr(write2response = true, value = "参数对比异常")
//	public Map compareParams(@PathVariable(value = "ids", required = false) String ids) throws Exception {
//		if (ids==null) {
//			throw new BusinessException("选择不能为空", FormStatusCode.PARAM_ILLEGAL);
//		}
//		String[] hua=ids.split("&");
//		String idstr=hua[0].replace(",","','");//获取到id列表
//		idstr="'"+idstr+"'";
//		String[] idss= idstr.split(",");
//		String[] mid=hua[0].split(",");
//		Map hmap=new HashMap();
//		//todo 拼接查询sql
//		List<Map<String,Object>> scoulums=new LinkedList<Map<String,Object>>();
//		Map<String,Object> map_cloumns=new HashMap<>();
//		map_cloumns.put("names","产品名称");
//		map_cloumns.put("title","参数名称");
//		map_cloumns.put("field","paramName");
//		scoulums.add(map_cloumns);
//		String sql="";
//		String sql_column="";
//		for (int i=1;i<idss.length;i++){
//			Map<String,Object> map_cloumns2=new HashMap<>();
//			map_cloumns2.put("title",""+idss[0]+"");
//			map_cloumns2.put("field",""+idss[i]);
//			scoulums.add(map_cloumns2);
//			if(i==idss.length-1){
//				sql_column=sql_column+" a"+mid[i]+".csz as a"+mid[i]+"";
//			}else{
//				sql_column=sql_column+" a"+mid[i]+".csz as a"+mid[i]+",";
//			}
//			sql=sql+" left join (select * from clcsb where materialid="+idss[i]+") a"+mid[i]+" on a"+mid[0]+".standardid=a"+mid[i]+".standardid";
//		}
//		System.out.println(sql);
//		if(hua.length>2){
//			String diss=hua[2].replace(",","','");//获取到id列表
//			diss="'"+diss+"'";
//			String[] typestr= diss.split(",");
//			String[] typestrs=hua[2].split(",");
//			String[] sid=hua[3].split(",");
//			for (int j=0; j<typestrs.length;j++){
//				Map<String,Object> map_cloumns2=new HashMap<>();
//				map_cloumns2.put("title","a"+typestrs[j]+"");
//				map_cloumns2.put("field","a"+typestrs[j]+"");
//				scoulums.add(map_cloumns2);
//				if(j==typestr.length-1){
//					sql_column=sql_column+", a"+sid[j]+".parameterValue as a"+sid[j]+" ";
//				}else{
//					sql_column=sql_column+", a"+sid[j]+".parameterValue as a"+sid[j]+" ";
//				}
//				sql =sql+" left join (select * from a_cl_parameter where materielid="+typestr[j]+" and standard='"+sid[j]+"') a"+sid[j]+" on a"+sid[j]+".standardsid=a"+mid[0]+".standardid";
//			}
//		}
//		String final_sql="select a"+mid[0]+".csm as csm,a"+mid[0]+".csz as a"+mid[0]+", "+sql_column+" from (select * from clcsb where materialid="+idss[0]+") a"+mid[0]+" "+sql+" group by a"+mid[0]+".standardid";
//		// 1、材料参数【选择材料对话框】
//		// 2、对比参数的条件判断【子查询中的条件判断】
//		// 3、增加标准参数【查询标准参数表】
//		// 4、隐藏相同项目由前端处理
////		String sql="";
//		System.out.println(final_sql);
//		HashMap<String,Object> parammap=new HashMap<>();
//		parammap.put("value",final_sql);
//		List<Map> listmap= this.paramCompareManager.queryCompare(parammap);
//		hmap.put("datelist",listmap);
//		hmap.put("clist",scoulums);
//		return hmap;
//	}

	@GetMapping("/compareParams/{s}/{name}")
	@ResponseBody
	public Map ratio (@PathVariable("s") String s,@PathVariable("name") String name) {
		String[] ss=s.split("&");
		String[] lvs=name.split("&");
		String[] lv=lvs[0].split(",");
		String idstr=ss[0];//产品id
//		String namestr=ss[1];//产品名称
		Map hmap=new HashMap();
		String []idss=idstr.split(",");
//		String [] nameDemo=namestr.split(",");
		//保存参数对比历史
//		this.clParamsmetaValueService.saveParamCompareHistory(clMetaCompareHistoryDO,idstr);
		List<Map<String,Object>> scoulums=new LinkedList<Map<String,Object>>();
		Map<String,Object> map_cloumns=new HashMap<>();
		map_cloumns.put("names","产品名称");
		map_cloumns.put("title","参数名称");
		map_cloumns.put("field","csm");
		scoulums.add(map_cloumns);
		Map<String,Object> map_cloumns1=new HashMap<>();
		map_cloumns1.put("title",""+lv[0]+"");
		map_cloumns1.put("field","a"+lv[0]);
		scoulums.add(map_cloumns1);
		String sql="";
		String sql_column="";
		for(int i=1;i<idss.length;i++){
			Map<String,Object> map_cloumns2=new HashMap<>();
			map_cloumns2.put("title",""+lv[i]+"");
			map_cloumns2.put("field",""+lv[i]+"a");
			scoulums.add(map_cloumns2);
			if(i==idss.length-1){
				sql_column=sql_column+" a"+lv[i]+".csz as "+lv[i]+"a";
			}else{
				sql_column=sql_column+" a"+lv[i]+".csz as "+lv[i]+"a,";
			}
			sql=sql+" left join (select * from clcsb where materielcodeid='"+idss[i]+"') a"+lv[i]+" on a"+lv[0] +".standardid=a"+lv[i]+".standardid ";
		}
		if(ss.length>1){
			String idlv=ss[1];
			if(!idlv.equals("null")) {
				String[] typestr = idlv.split(",");
				String[] sid = lvs[1].split(",");
				for (int j = 0; j < typestr.length; j++) {
					Map<String, Object> map_cloumns2 = new HashMap<>();
					map_cloumns2.put("title", "" + sid[j] + "");
					map_cloumns2.put("field", "a" + sid[j] + "a");
					scoulums.add(map_cloumns2);
					if (j == typestr.length - 1) {
						sql_column = sql_column + ", a" + sid[j] + ".parameterValue as a" + sid[j] + "a ";
					} else {
						sql_column = sql_column + ", a" + sid[j] + ".parameterValue as a" + sid[j] + "a ";
					}
					sql = sql + " left join (select * From a_cl_parameter where materielid='" + typestr[j] + "' ) a" + sid[j] + " on a" + sid[j] + ".parameterid=a" + lv[0] + ".standardid";
				}
			}
		}
//		String type="";
////		String mateCode="";
////		if(!type.equals("")){
////			String[]  typestr=type.split(",");
////			for(int j=0;j<typestr.length;j++){
////				sql=sql="LEFT JOIN (SELECT * FROM a_cl_paramscode_value WHERE materielId ="+mateCode+" AND `type`='"+typestr[j]+"') ab"+typestr[j]+" ON a366.parameterid =ab"+typestr[j]+".paramsmetaId";
////			}
////		}
		//LEFT JOIN (SELECT * FROM a_cl_paramscode_value WHERE materielId = '01.01.01' AND `type`='低标准') a370 ON a366.parameterid =a370.paramsmetaId


		String final_sql="select a"+lv[0] +".csm as csm,a"+lv[0]+".csz as a"+lv[0]+", "+sql_column+" from (select * from clcsb where materielcodeid='"+idss[0]+"') a"+lv[0] +"  "+sql;
		System.out.println(final_sql);

		HashMap<String,Object> hparams=new HashMap<>();
		hparams.put("value",final_sql);

		List<Map>  hlist= this.paramCompareManager.queryCompare(hparams);

		hmap.put("datalist",hlist);
		hmap.put("clist",scoulums);
		return hmap;

	}



	/**
	 * vu清单更改
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/updatevelist/{id}")
	@CatchErr(write2response = true, value = "参数对比异常")
	public ResultMsg updatevelist(@PathVariable(value = "id", required = false) String id) throws Exception {
		if (id==null) {
			throw new BusinessException("选择不能为空", FormStatusCode.PARAM_ILLEGAL);
		}

		//todo 根据旧id修改ve清单

		return getSuccessResult("材料收藏成功！");
	}

	@GetMapping("/identical/{id}")
	public String identical(@PathVariable("id") String id){
		String[] num=id.split(",");
		if(num.length>1){
			String nu="";
			String n="";
			for (int i=0;i<num.length;i++){
				nu=num[i].substring(0,num[i].lastIndexOf("."));
				if(i!=num.length-1) {
					n = num[i + 1].substring(0, num[i].lastIndexOf("."));
					if(nu.equals(n)){
						n=nu;
					}else{
						return "不进入";
					}
				}else{
					if(n.equals(nu)){
						n=nu;
					}else{
						return "不进入";
					}
				}
			}
			return "进入";
		}else{
			return "不能只选择一项";
		}
	}

}
